/*******************************************************************
 * 外语翻译平台(translate) 
 *
 * 成都一亿信息技术公司 版权所有
 * 
 * Copyright (C) 2010 Transer Information Technology Co., Ltd.
 *
 * @Version : 1.0  2011-1-12 下午12:09:43
 *
 * AddLoginLogJob.java
 *
 *******************************************************************/
package com.transer.job;

import org.apache.log4j.Logger;

import com.transer.persistance.bean.LoginLog;
import com.transer.persistance.dao.DatabaseDao;
import com.transer.persistance.dao.ILogDao;
import com.transer.persistance.exception.TranserDatabaseException;

/**
 * 添加登陆日志JOB
 * 
 * @author Mark
 * 
 * @version 1.0 ywengineer@gmail.com
 * 
 */
public class AddLoginLogJob implements Runnable {
	private static final Logger logger = Logger.getLogger(AddLoginLogJob.class);
	// 数据库操作
	private DatabaseDao database;
	// 日志
	private LoginLog log;

	public AddLoginLogJob(DatabaseDao dao, LoginLog log) {
		this.database = dao;
		this.log = log;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Runnable#run()
	 */
	@Override
	public void run() {
		try {
			if (database != null && database instanceof ILogDao && log.getUser() != null) {
				// 日志操作
				ILogDao logDao = (ILogDao) database;
				// 获取最近一次登陆日期
				LoginLog last = logDao.getLastLogin(log.getUser().getId());
				// 如果存在最近一次登陆日志
				if (last != null) {
					// 设置上次登陆时间
					log.getUser().setLastLoginTime(last.getLoginTime());
				}
				// 添加当前登陆日志
				logDao.addLoginLog(log);
			}
		} catch (TranserDatabaseException e) {
			e.printStackTrace();
			logger.error(e.getMessage(), e);
		}
	}

}
